package com.example.composableexample.animate

import android.graphics.Path
import androidx.annotation.FloatRange
import androidx.compose.material3.SwitchDefaults
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.asComposePath
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection

private fun Modifier.circularClip(
    @FloatRange(from = 0.0, to = 1.0) progress: Float
) = clip(CircularRevealShape(progress))

private class CircularRevealShape(
    @FloatRange(from = 0.0, to = 1.0) private val progress: Float
) : Shape {
    override fun createOutline(
        size: Size,
        layoutDirection: LayoutDirection,
        density: Density
    ): Outline {
        val thumbPaddingPx = with(density) { SwitchDefaults.IconSize }
        val thumbSizePx = with(density) { SwitchDefaults.IconSize }

        val x = thumbPaddingPx + (thumbSizePx / 2f)
        val y = size.height / 2

        val radius = size.width * 2 * progress
        return Outline.Generic(Path().apply {
            addCircle(
                y,
                y,
                radius,
                Path.Direction.CW
            )
        }.asComposePath())
    }
}